-- ========================================================================
-- chinook database example queries
-- ========================================================================
-- Chinook is a digital-media store, modelled here as a fully RELATIONAL
-- CoraDB database (11 tables, standard ANSI SQL -- joins, GROUP BY,
-- subqueries, self-joins).
-- Catalogue:  artist -> album -> track;  track -> genre / media_type.
-- Sales:      customer -> invoice -> invoice_line -> track.
-- People:     customer -> employee (support rep);  employee -> employee (manager).
-- Playlists:  playlist <-> track  (via playlist_track).
-- ========================================================================

------------------------------------------------------------------------
-- Basics
------------------------------------------------------------------------

-- How many artists, albums, tracks, customers and invoices are there?
SELECT COUNT(*) AS artists FROM artist;
SELECT COUNT(*) AS albums FROM album;
SELECT COUNT(*) AS tracks FROM track;
SELECT COUNT(*) AS customers FROM customer;
SELECT COUNT(*) AS invoices FROM invoice;

-- Every album by Led Zeppelin.
SELECT ar.name AS artist, al.title AS album
FROM artist ar
JOIN album al ON al.artist_id = ar.artist_id
WHERE ar.name = 'Led Zeppelin'
ORDER BY al.title;

-- The tracks on the album "Let There Be Rock", in order, with their length in minutes.
SELECT t.name AS track, ROUND(t.milliseconds / 60000.0, 2) AS minutes
FROM track t
JOIN album al ON al.album_id = t.album_id
WHERE al.title = 'Let There Be Rock'
ORDER BY t.track_id;

------------------------------------------------------------------------
-- Catalogue
------------------------------------------------------------------------

-- Number of tracks in each genre.
SELECT g.name AS genre, COUNT(*) AS tracks
FROM genre g
JOIN track t ON t.genre_id = g.genre_id
GROUP BY g.name
ORDER BY tracks DESC;

-- The 10 artists with the most albums in the catalogue.
SELECT ar.name AS artist, COUNT(*) AS albums
FROM artist ar
JOIN album al ON al.artist_id = ar.artist_id
GROUP BY ar.artist_id, ar.name
ORDER BY albums DESC
LIMIT 10;

-- The 10 longest tracks (with their album and genre).
SELECT t.name AS track, al.title AS album, g.name AS genre,
       ROUND(t.milliseconds / 60000.0, 2) AS minutes
FROM track t
LEFT JOIN album al ON al.album_id = t.album_id
LEFT JOIN genre g ON g.genre_id = t.genre_id
ORDER BY t.milliseconds DESC
LIMIT 10;

-- Track count and average length per media type.
SELECT mt.name AS media_type, COUNT(*) AS tracks,
       ROUND(AVG(t.milliseconds) / 60000.0, 2) AS avg_minutes
FROM media_type mt
JOIN track t ON t.media_type_id = mt.media_type_id
GROUP BY mt.name
ORDER BY tracks DESC;

-- The 10 albums with the most tracks (with their artist).
SELECT al.title AS album, ar.name AS artist, COUNT(*) AS tracks
FROM album al
JOIN artist ar ON ar.artist_id = al.artist_id
JOIN track t ON t.album_id = al.album_id
GROUP BY al.album_id, al.title, ar.name
ORDER BY tracks DESC
LIMIT 10;

------------------------------------------------------------------------
-- Sales & revenue
------------------------------------------------------------------------

-- Total revenue billed to each country (top 10).
SELECT billing_country AS country, ROUND(SUM(total), 2) AS revenue, COUNT(*) AS invoices
FROM invoice
GROUP BY billing_country
ORDER BY revenue DESC
LIMIT 10;

-- The 10 customers who have spent the most.
SELECT c.first_name, c.last_name, c.country, ROUND(SUM(i.total), 2) AS spent
FROM customer c
JOIN invoice i ON i.customer_id = c.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name, c.country
ORDER BY spent DESC
LIMIT 10;

-- Revenue by genre (which styles of music actually sell).
SELECT g.name AS genre, ROUND(SUM(il.unit_price * il.quantity), 2) AS revenue
FROM invoice_line il
JOIN track t ON t.track_id = il.track_id
JOIN genre g ON g.genre_id = t.genre_id
GROUP BY g.name
ORDER BY revenue DESC;

-- The 10 best-selling tracks by units sold (ties broken by revenue, then name).
SELECT t.name AS track, SUM(il.quantity) AS units, ROUND(SUM(il.unit_price * il.quantity), 2) AS revenue
FROM invoice_line il
JOIN track t ON t.track_id = il.track_id
GROUP BY t.track_id, t.name
ORDER BY units DESC, revenue DESC, track
LIMIT 10;

-- The 10 best-selling artists by revenue (through album -> track -> line).
SELECT ar.name AS artist, ROUND(SUM(il.unit_price * il.quantity), 2) AS revenue
FROM invoice_line il
JOIN track t ON t.track_id = il.track_id
JOIN album al ON al.album_id = t.album_id
JOIN artist ar ON ar.artist_id = al.artist_id
GROUP BY ar.artist_id, ar.name
ORDER BY revenue DESC
LIMIT 10;

-- Sales per calendar year. (Use YEAR()/TO_CHAR() on dates: SUBSTR() on a DATETIME
-- works on its default text form '12:00:00.000 AM 01/01/2009', not 'YYYY-MM-DD'.)
SELECT YEAR(invoice_date) AS `year`, COUNT(*) AS invoices, ROUND(SUM(total), 2) AS revenue
FROM invoice
GROUP BY YEAR(invoice_date)
ORDER BY `year`;

------------------------------------------------------------------------
-- Customers & staff
------------------------------------------------------------------------

-- How many customers does each support rep look after, and how much have
-- those customers spent in total?
SELECT e.first_name, e.last_name,
       COUNT(DISTINCT c.customer_id) AS customers,
       ROUND(SUM(i.total), 2) AS revenue
FROM employee e
JOIN customer c ON c.support_rep_id = e.employee_id
JOIN invoice i ON i.customer_id = c.customer_id
GROUP BY e.employee_id, e.first_name, e.last_name
ORDER BY revenue DESC;

-- The management chain: each employee and the manager they report to.
SELECT e.first_name || ' ' || e.last_name AS employee, e.title,
       m.first_name || ' ' || m.last_name AS reports_to
FROM employee e
LEFT JOIN employee m ON m.employee_id = e.reports_to
ORDER BY e.employee_id;

-- Average invoice value, and largest single invoice.
SELECT ROUND(AVG(total), 2) AS avg_invoice, MAX(total) AS max_invoice
FROM invoice;

------------------------------------------------------------------------
-- Playlists
------------------------------------------------------------------------

-- The number of tracks in each playlist. (The original Chinook data has duplicate
-- playlist names -- e.g. two "Music" playlists, ids 1 and 8 -- so the id is shown.)
SELECT p.playlist_id, p.name AS playlist, COUNT(*) AS tracks
FROM playlist p
JOIN playlist_track pt ON pt.playlist_id = p.playlist_id
GROUP BY p.playlist_id, p.name
ORDER BY tracks DESC;

-- The genres that appear on the "Music" playlist (top 10). Filter on the id: the
-- name 'Music' matches two playlists (1 and 8) and would count every track twice.
SELECT g.name AS genre, COUNT(*) AS tracks
FROM playlist p
JOIN playlist_track pt ON pt.playlist_id = p.playlist_id
JOIN track t ON t.track_id = pt.track_id
JOIN genre g ON g.genre_id = t.genre_id
WHERE p.playlist_id = 1
GROUP BY g.name
ORDER BY tracks DESC
LIMIT 10;


